package com.qianfeng.qlm.handforireader.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.qianfeng.qlm.handforireader.R;
import com.qianfeng.qlm.handforireader.activity.CategoryDetailActivity;
import com.qianfeng.qlm.handforireader.adapter.CategoryAdapter;
import com.qianfeng.qlm.handforireader.model.Category;
import com.qianfeng.qlm.handforireader.model.CategoryDetail;
import com.qianfeng.qlm.handforireader.util.Constant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by IntelliJ IDEA
 * User:tadhg
 * Date:2015/3/23
 * Email:tadhg299@163.com
 */
public class CategoryFragment extends Fragment implements View.OnClickListener {

    private RequestQueue mRequestQueue;

    private List<Category> data;
    private CategoryAdapter adapter;
    private RecyclerView recyclerView;

    public CategoryFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category, container, false);

        recyclerView = (RecyclerView) view.findViewById(R.id.category_recycle);
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);

        mRequestQueue = Volley.newRequestQueue(getActivity());

        data = new ArrayList<>();
        adapter = new CategoryAdapter(getActivity(), data, this, mRequestQueue);
        recyclerView.setAdapter(adapter);



        getCategory();

        return view;
    }

    @Override
    public void onClick(View v) {
        int position = recyclerView.getChildPosition(v);
//        Toast.makeText(getActivity(),"position" + position,Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getActivity(), CategoryDetailActivity.class);
        Bundle args = new Bundle();
        args.putSerializable("category",data.get(position));
        intent.putExtras(args);
        startActivity(intent);
    }

    public void getCategory(){
        JsonObjectRequest jsonRequest = new JsonObjectRequest(
                Request.Method.GET,
                Constant.URL_CATEGORY,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("comic_tag_arr");
                            String json = jsonArray.toString();
                            Gson gson = new Gson();
                            List<Category> list = new ArrayList<>();
                            list = Arrays.asList(gson.fromJson(json,Category[].class));
                            data.addAll(list);
                            adapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("error",error.toString());
                    }
                }
        );
        mRequestQueue.add(jsonRequest);
    }

}
